<template>
  <view class="container">
    <web-view v-if="webViewSrc"
              :src="webViewSrc"
              :fullscreen="fullscreen"
              @message="onMessage"
    />
  </view>
</template>

<script>
import order_api from "@/common/order_api";

export default {
  data() {
    return {
      bsform: '',
      webViewSrc: '',
      fullscreen: true,
      timer: null,
    };
  },
  onLoad(options) {
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
    console.log(options);
    this.bsfrom = options.bsfrom || '';
    //拼接其他参数
    let url = options.url;
    for (let key in options) {
      if (key !== 'url') {
        url += `&${key}=${options[key]}`
      }
    }
    console.log(this.bsfrom, url)

    if (this.bsfrom == 'order-pay' || this.bsfrom == 'recharge') {
      console.log('order-pay')
      //定时器，检查支付结果
      this.timer = setInterval(() => {
        console.log('timer:' + options.order_sn)
        order_api.pay_check({
          order_sn: options.order_sn,
        }).then(res => {
          console.log('timer:' + options.order_sn, res)
          if (res.code === 0) {
            clearInterval(this.timer);
            if (this.bsfrom == 'order-pay') {
              uni.navigateTo({
                url: '/pages/packageA/order/detail?order_sn=' + options.order_sn
              })
            } else if (this.bsfrom == 'recharge') {
              uni.navigateTo({
                url: '/pages/packageA/user/recharge/history'
              })
            }

          }
        })

      }, 2000)
    }

    this.webViewSrc = url;
    //设置标题
    uni.setNavigationBarTitle({
      title: options.title || 'Ｈ５',
    });
  },
  onUnload() {
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  },
  destroyed() {
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  },
  methods: {
    onMessage(e) {
      if (e.detail.data === 'close') {
        uni.navigateBack();
      }
    }
  },
}
</script>

<style lang="scss">
.container {
  width: 100%;
  height: 100%;
}
</style>
